﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Evogame.Settings;

namespace EvoGame.Ceeforee
{
	public partial class MainWin
	{
		public ConfigFile ModConfig = new ConfigFile();

		public void LoadSettings()
		{
			StringBuilder message = new StringBuilder();
			bool err = false;

			try
			{
				ModConfig.LoadFromFile(String.Format("{0}\\{1}.ini", LoadedMod.Dir["Mod"]["Root"], LoadedMod.Name));
			}
			catch
			{
				ModConfig = new ConfigFile();
				ModConfig.AddSettingsGroup("CONFIG");
			}
			scenarioFile.Items.Clear();
			DirectoryInfo publicMaps = new DirectoryInfo(LoadedMod.Dir["Mod"]["Root"] + "\\PublicMaps");

			foreach (KeyValuePair<string, object> option in general_Options)
			{
				try
				{

					if (option.Value.GetType() == typeof(TextBox))
					{
						if (option.Key != "ImageFile")
						{

							TextBox tmp_TB = (TextBox)option.Value;
							tmp_TB.Text = ModConfig.SettingGroups["CONFIG"].Settings[option.Key].GetValueAsString();

						}
						else
						{
							if (ModConfig.SettingGroups["CONFIG"].Settings[option.Key].GetValueAsString() == "0")
							{
								imageFile.Text = "";
							}
							else
							{
								imageFile.Text = ModConfig.SettingGroups["CONFIG"].Settings[option.Key].GetValueAsString();
							}

						}
					}
					else if (option.Value.GetType() == typeof(CheckBox))
					{
						CheckBox tmp_CB = (CheckBox)option.Value;
						if (option.Key == "ModularLoading")
						{
							tmp_CB.Checked = ModConfig.SettingGroups["CONFIG"].Settings[option.Key].GetValueAsBool();
							loadedMod.Modular = tmp_CB.Checked;
						}
						else if (option.Key != "ForceScenario")
						{
							tmp_CB.Checked = ModConfig.SettingGroups["CONFIG"].Settings[option.Key].GetValueAsBool();
						}
						else
						{
							if (ModConfig.SettingGroups["CONFIG"].Settings[option.Key].GetValueAsString() == "0")
							{
								forceScenario.Checked = false;
								try
								{
									scenarioFile.Items.AddRange(publicMaps.GetFiles());
								}
								catch (Exception e)
								{
									Debug = e.Message;
								}
								scenarioFile.Text = "";
								scenarioFile.Enabled = false;
							}
							else
							{
								forceScenario.Checked = true;
								scenarioFile.Items.AddRange(publicMaps.GetFiles());
								scenarioFile.Text = ModConfig.SettingGroups["CONFIG"].Settings[option.Key].GetValueAsString();
								scenarioFile.Enabled = true;
							}
						}
					}
				}
				catch
				{
					err = true;
					try
					{
						message.AppendFormat("{2}Setting \"{0}\" is malformed (Value = \"{1}\")", option.Key, ModConfig.SettingGroups["CONFIG"].Settings[option.Key].GetValueAsString(),Environment.NewLine);
					}
					catch
					{
						message.AppendFormat("{1}Setting \"{0}\" is missing", option.Key, Environment.NewLine);
						if (option.Value.GetType() == typeof(TextBox))
						{
							ModConfig.SettingGroups["CONFIG"].AddSetting(option.Key, " ");
						}
						else if (option.Value.GetType() == typeof(CheckBox))
						{
							ModConfig.SettingGroups["CONFIG"].AddSetting(option.Key, 0);
						}
					}
				}
			}
			if (err)
			{
				Debug = "Warning Displayed: " + Environment.NewLine + message.ToString();
				MessageBox.Show(message.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}

		}
		
		public void SaveSettings()
		{
			foreach (KeyValuePair<string, object> option in general_Options)
			{
				if (option.Value.GetType() == typeof(TextBox))
				{
					TextBox tmp_TB = (TextBox)option.Value;
					if (option.Key == "ImageFile" && tmp_TB.Text == "")
					{
						ModConfig.SettingGroups["CONFIG"].Settings[option.Key].SetValue(0);
					}
					else
					{
						ModConfig.SettingGroups["CONFIG"].Settings[option.Key].SetValue(tmp_TB.Text);
					}
				}
				else if (option.Value.GetType() == typeof(CheckBox))
				{
					CheckBox tmp_CB = (CheckBox)option.Value;
					if (option.Key == "ForceScenario" && tmp_CB.Checked)
					{
						ModConfig.SettingGroups["CONFIG"].Settings[option.Key].SetValue(scenarioFile.Text);
					}
					else
					{
						ModConfig.SettingGroups["CONFIG"].Settings[option.Key].SetValue(Convert.ToInt32(tmp_CB.Checked));
					}
				}
			}

			ModConfig.Save(String.Format("{0}\\{1}.ini", LoadedMod.Dir["Mod"]["Root"], LoadedMod.Name));
		}

		private void forceScenario_State_Changed(object sender, EventArgs e)
		{
			scenarioFile.Enabled = forceScenario.Checked;
		}

		private void modularLoading_State_Changed(object sender, EventArgs e)
		{
			loadedMod.Modular = modularXML.Checked;
		}
	}
}
